package com.jcl.core.security.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jcl.core.resp.ApiResponse;
import com.jcl.core.security.entity.ResultEnum;
import com.jcl.core.utils.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ResultUtil
 * @Author yyliu
 * @Date 2018/12/18 11:35
 * @Version 1.0
 **/
public final class ResultUtil implements Serializable {

    private static final long serialVersionUID = 1725159680599612404L;

    public static void success(HttpServletResponse response, String message, Integer code, Object obj) {

        String result = result(ApiResponse.TYPE_SUCCESS, code, message, obj);
        responseOutWithJson(response, result);
    }

    public static void success(HttpServletResponse response, String message, Integer code, String token) {

        String result = result(ApiResponse.TYPE_SUCCESS, code, message, tokenMap(token));
        responseOutWithJson(response, result);
    }

    public static void success(HttpServletResponse response, String message, String token) {

        String result = result(ApiResponse.TYPE_SUCCESS, ResultEnum.SUCCESS.getCode(), message, tokenMap(token));
        responseOutWithJson(response, result);
    }

    public static void success(HttpServletResponse response) {

        String result = result(ApiResponse.TYPE_SUCCESS, ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getMessage(), null);
        responseOutWithJson(response, result);
    }

    public static void failure(HttpServletResponse response, Integer code, String message, Object data) {

        String result = result(ApiResponse.TYPE_FAILURE, code, message, data);
        responseOutWithJson(response, result);
    }

    public static void failure(HttpServletResponse response, Integer code, String message) {
        String result = result(ApiResponse.TYPE_FAILURE, code, message, null);
        responseOutWithJson(response, result);
    }


    public static String result(String type, Integer code, String message, Object obj) {

        return resultJsonStr(resultMap(type, code, message, obj));
    }

    private static Map<String, Object> resultMap(String type, Integer code, String message, Object obj) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.hasLength(type)) {
            map.put("type", type);
        }
        if (null != code) {
            map.put("code", code);
        }
        if (StringUtils.hasLength(message)) {
            map.put("message", message);
        }
        if (null != obj) {
            map.put("data", obj);
        } else {
            map.put("data", new HashMap());
        }
        return map;
    }

    private static Map<String, Object> tokenMap(String token) {
        Map<String, Object> map = new HashMap<>();
        if(StringUtils.hasLength(token)){
            map.put("token", token);
        }
        return map;
    }


    private static String resultJsonStr(Map<String, Object> response) {

        return JSON.toJSONString(response);
    }

    protected static void responseOutWithJson(HttpServletResponse response,
                                       String jsonStr) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.append(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

}
